package com.university.report.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.university.report.common.result.Result;
import com.university.report.common.result.ResultCodeEnum;
import com.university.report.model.entity.ReportInfo;
import com.university.report.model.entity.UserInfo;
import com.university.report.model.vo.ReportInfoVO;
import com.university.report.model.vo.UserInfoVO;
import com.university.report.user.mapper.UserInfoMapper;
import com.university.report.user.service.ReportInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Jinondo
 * @since 2021-06-09
 */
@Api(value = "service-user模块", tags = "上报信息控制器")
@RestController
@RequestMapping("/api")
public class ReportInfoController {

    @Autowired
    ReportInfoService reportInfoService;
    @Autowired
    UserInfoMapper userInfoMapper;

    @ApiOperation(value = "删除数据信息")
    @DeleteMapping("/deleteReportInfo")
    public Result deleteReportInfo(Integer id){
        boolean success = reportInfoService.removeById(id);
        if (success){
            return Result.ok();
        }
        return Result.fail();
    }




    @ApiOperation(value = "健康上报")
    @PutMapping("/report")
    public Result report(@RequestBody ReportInfo reportInfo){

        System.out.println(reportInfo);


//        QueryWrapper queryWrapper=new QueryWrapper();
//
//        queryWrapper.eq("user_id",reportInfo.getUserId());

        UserInfoVO userInfoVO=userInfoMapper.selectByUserId(reportInfo.getUserId());
        if (userInfoVO.getReportStatus()==1)return Result.fail().message("已打卡，不需要重复提交");
        reportInfoService.reportMQ(reportInfo);
        return Result.ok("健康上报成功（未校验）");
//        ReportInfoService.ReportStatus status = reportInfoService.report(reportInfo);
//        if (status == ReportInfoService.ReportStatus.SUCCESS){
//            return Result.ok(status.getMessage());
//        }
//        return Result.fail(status.getMessage());
    }

    @ApiOperation("总体筛选导出")
    @GetMapping("/exportInfoByCondition")
    public void exportInfoByCondition(HttpServletResponse response,
                                      @RequestParam(name = "currentPage") Integer currentPage,
                                     @RequestParam(name = "department", required = false) @ApiParam("学院") String department,
                                     @RequestParam(name = "dateString", required = false) @ApiParam("时间 yyyy-MM-dd") String dateString,
                                     @RequestParam(name = "unHealth", required = false) @ApiParam("筛选体温异常或身体状况异常 0-不筛选 1-筛选") Integer healthCon,
                                     @RequestParam(name = "impRegion", required = false) @ApiParam("查找最近15天到达或途径疫情防控重点区域记录 0-不筛选 1-筛选") Integer impRegion){
        reportInfoService.exportDataByCond(response,currentPage,department,dateString,healthCon,impRegion);
    }

    /**
     * 根据以下筛选列表
     * 1. 学院
     * 2. 时间
     * 3. 筛选体温异常或身体状况异常
     * 4. 查找最近15天到达或途径疫情防控重点区域记录
     * @param department
     * @param dateString
     * @param healthCon
     * @param impRegion
     */
    @ApiOperation("总体筛选列表")
    @GetMapping("/getInfoByCondition")
    public Result getInfoByCondition(@RequestParam(name = "currentPage") Integer currentPage,
                                     @RequestParam(name = "department", required = false) @ApiParam("学院") String department,
                                   @RequestParam(name = "dateString", required = false) @ApiParam("时间 yyyy-MM-dd") String dateString,
                                   @RequestParam(name = "unHealth", required = false) @ApiParam("筛选体温异常或身体状况异常 0-不筛选 1-筛选") Integer healthCon,
                                   @RequestParam(name = "impRegion", required = false) @ApiParam("查找最近15天到达或途径疫情防控重点区域记录 0-不筛选 1-筛选") Integer impRegion){
        Map<String, Object> map = reportInfoService.getInfoByCond(currentPage, department, dateString, healthCon, impRegion);
        if (map!=null && map.get("infos")==null){
            return Result.fail(ResultCodeEnum.DATA_NOT_EXIST);
        }
        return Result.ok(map);
    }


    @ApiOperation(value = "按学院/日期导出健康上报信息表", produces = "application/octet-stream")
    @GetMapping("/exportInfoByDateDep")
    public void exportInfo(HttpServletResponse response,
                           @RequestParam(name = "department",required = false) @ApiParam("学院") String department,
                           @RequestParam(name = "dateString",required = false) @ApiParam("时间 yyyy-MM-dd") String dateString){
        reportInfoService.exportData(response,department,dateString);
    }


    /**
     * 根据院和时间查询上报信息
     * @param department
     * @return
     */
    @ApiOperation(value = "根据院和时间查询上报信息")
    @GetMapping("/getInfoByDep")
    public Result getInfoByDep(@RequestParam("department") @ApiParam("部门") String department,
                               @RequestParam(name = "dateString",required = false) @ApiParam("时间 yyyy-MM-dd (可以不传)") String dateString){
        List<ReportInfoVO> infoList = reportInfoService.getInfoByDepNDate(department,dateString);
        return Result.ok(infoList);
    }

    @Deprecated
    @ApiOperation(value = "查询该时间后的上报信息")
    @GetMapping("/getInfoAfterTime")
    public Result getInfoAfterTime(@ApiParam("时间 yyyy-MM-dd") String timeString){
        List infoList;
        try {
            infoList = reportInfoService.getInfoByTime(timeString);
        } catch (IllegalArgumentException exception){
            return Result.fail(ResultCodeEnum.TIMEFORMAT_ERROR);
        }
        return Result.ok(infoList);
    }



    @ApiOperation(value = "查询今天的上报信息")
    @GetMapping("/getInfoToday")
    public Result getInfoToday(@RequestParam("currentPage") @ApiParam("获取某一页码的数据")  Integer currentPage){
        Map<String, Object> map = reportInfoService.getInfoToday(currentPage);
        if (map!=null && map.isEmpty()){
            return Result.fail(ResultCodeEnum.DATA_NOT_EXIST);
        }
        return Result.ok(map);
    }

    @ApiOperation(value = "查询某一天的上报信息")
    @GetMapping("/getInfoByDate")
    public Result getInfoByDate(@RequestParam("currentPage") @ApiParam("获取某一页码的数据")Integer currentPage,
                                @RequestParam("timeString") @ApiParam("时间 yyyy-MM-dd") String timeString){
        Map<String, Object> map = reportInfoService.getInfoByDate(currentPage, timeString);
        if (map!=null && map.isEmpty()){
            return Result.fail(ResultCodeEnum.DATA_NOT_EXIST);
        }
        return Result.ok(map);
    }

    @ApiOperation(value = "查找特定日期体温/健康状况异常的记录")
    @GetMapping("/getUnusualHealthDate")
    public Result getUnusualHealthDate(@RequestParam("timeString") @ApiParam("时间 yyyy-MM-dd") String timeString){
        List<ReportInfoVO> unusualHealthDate = reportInfoService.getUnusualHealthDate(timeString);
        if (unusualHealthDate!=null && unusualHealthDate.isEmpty()){
            return Result.fail(ResultCodeEnum.DATA_NOT_EXIST);
        }
        return Result.ok(unusualHealthDate);
    }

    @ApiOperation(value = "查找今天日期体温/健康状况异常的记录")
    @GetMapping("/getIllnessToday")
    public Result getIllnessToday(){
        List<ReportInfoVO> unusualHealthToday = reportInfoService.getUnusualHealthToday();
        if (unusualHealthToday!=null && unusualHealthToday.isEmpty()){
            return Result.fail(ResultCodeEnum.DATA_NOT_EXIST);
        }
        return Result.ok(unusualHealthToday);
    }

    @ApiOperation(value = "查找最近15天到达或途径疫情防控重点区域记录")
    @GetMapping("/getImpRegionByDate")
    public Result getImpRegionByDate(@RequestParam(name = "dateString", required = false)String dateString){
        List<ReportInfoVO> reportInfoVOS = reportInfoService.getImpRegion(dateString);
        if (reportInfoVOS!=null && reportInfoVOS.isEmpty()){
            return Result.fail(ResultCodeEnum.DATA_NOT_EXIST);
        }
        return Result.ok(reportInfoVOS);
    }

    @ApiOperation(value = "通过用户ID查询上报信息记录-排序")
    @GetMapping("/getInfoByUserIdSort")
    public Result getInfoByUserIdSort(@RequestParam("userId")Integer userId){
        List<ReportInfoVO> reportInfoVOS = reportInfoService.getReportInfoByUserIdSort(userId);
        if (reportInfoVOS!=null && reportInfoVOS.isEmpty()){
            return Result.fail(ResultCodeEnum.DATA_NOT_EXIST);
        }
        return Result.ok(reportInfoVOS);
    }

    @ApiOperation(value = "通过用户ID查询最近一次上报记录-用于回填")
    @GetMapping("/getOneInfoByUserId")
    public Result getOneInfoByUserId(@RequestParam("userId")Integer userId){
        ReportInfoVO infoVO = reportInfoService.getOneInfoByUserId(userId);
        if (infoVO==null){
            return Result.fail(ResultCodeEnum.DATA_NOT_EXIST);
        }
        return Result.ok(infoVO);
    }

    // ==================================================================

    @ApiOperation(value = "仅用于后台插入数据")
    @PutMapping("/insertTestData")
    public Result insertTestData(String key){
        if("jinondocyt".equals(key)){
            ReportInfo reportInfo = ReportInfo.insertTest();
            reportInfoService.save(reportInfo);
            return Result.ok("添加成功");
        }
        return Result.fail(ResultCodeEnum.DEPLOYER_PERMIT_API);
    }
}

